//
//  EGShoppingCartParticularView.m
//  Orchard
//
//  Created by ios001 on 16/8/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGShoppingCartParticularView.h"
#import "EGaddressCell.h"
#import "EGTimeCell.h"
#import "EGgoodsCell.h"
#import "EGShopCartManager.h"

@interface EGShoppingCartParticularView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *particularView;

@property (nonatomic,strong) UIView    *bottomView;

@property (nonatomic,strong) UIButton  *buyBtn;

@end

@implementation EGShoppingCartParticularView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.particularView.delegate = self;
        self.particularView.dataSource = self;
        self.particularView.allowsSelection = NO;
        [self.particularView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"particularCell"];
        [self.particularView registerClass:[EGaddressCell class] forCellReuseIdentifier:@"addressCell"];
        [self.particularView registerClass:[EGTimeCell class] forCellReuseIdentifier:@"timeCell"];
        [self.particularView registerClass:[EGgoodsCell class] forCellReuseIdentifier:@"goodsCell"];
        self.particularView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    return self;
}

- (void)layoutSubviews{
    [self setLayout];
}

- (void)setupUI{
    [self addSubview:self.particularView];
    
    [self addSubview:self.bottomView];
    [self.bottomView addSubview:self.buyBtn];
    
    [self.buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }

- (void)setLayout{

//    
    [self.particularView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(64);
        make.bottom.trailing.leading.equalTo(self);
    }];

    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.trailing.leading.equalTo(self);
        make.height.offset(49);
    }];
    
    [self.buyBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(self.bottomView);
        make.width.equalTo(100);
    }];
}

- (IBAction)buyBtnClick:(id)sender{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushVc" object:nil];
}

#pragma mark - 方法
// 商品加入购物车
- (void)addGoods{
    
}

// 商品移除购物车
- (void)removeGoods{
    
}

#pragma mark - tableView数据源代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        EGaddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressCell" forIndexPath:indexPath];
        
        //
        //        self.particularView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cell;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        EGTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
        return cell;
    }else{
    EGgoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodsCell" forIndexPath:indexPath];
        
        return cell;
    }

}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    return self.addressView;
//}

/*
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 80;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        return 150;
    }
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 20;
    }
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

#pragma mark - 懒加载控件
- (UIView *)particularView{
    if (_particularView == nil) {
        _particularView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    }
    return _particularView;
}


- (UIView *)bottomView{
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (UIButton *)buyBtn{
    if (_buyBtn == nil) {
        _buyBtn = [[UIButton alloc] init];
        [_buyBtn setTitle:@"选好啦" forState:UIControlStateNormal];
        _buyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _buyBtn.backgroundColor = ThemeColor;
        [_buyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _buyBtn;
}

@end
